﻿@page "/Grid/DataBinding/LargeDataInstantFeedback"
@layout DataProviderAccessArea<IRentInfoDataProvider>
@using BlazorDemo.Pages.Grid.Filtering
@using static BlazorDemo.Pages.Grid.Filtering.Grid_Filtering_ColumnFilterMenu_CustomRange;

<DemoPageSectionComponent Id="Grid-DataBinding-LargeDataInstantFeedback" ShowSizeMode="true">
    <ChildContentWithParameters Context="Params">
        @using DevExpress.Data.Linq
        @inherits OwningComponentBase
        @implements IDisposable
        @inject Microsoft.Extensions.Configuration.IConfiguration Configuration
       
        <DataProviderAccessAreaContainer DataProvider="@RentInfoDataProvider">
        <div class="grid-container">
            <DxGrid  @ref="Grid"
                    Data="@InstantFeedbackSource"
                    ShowGroupPanel="true"
                    VirtualScrollingEnabled="VirtualScrollingEnabled" PageSize="20"
                    FilterMenuButtonDisplayMode="GridFilterMenuButtonDisplayMode.Always"
                    SizeMode="Params.SizeMode"
                    ColumnResizeMode="GridColumnResizeMode.NextColumn"
                    TextWrapEnabled="false">
                <Columns>                   
                    <DxGridDataColumn FieldName="State" MinWidth="80" Width="10%" />
                    <DxGridDataColumn FieldName="Area" Width="12%" />
                    <DxGridDataColumn FieldName="City" Caption="County" MinWidth="100" Width="17%" />
                    <DxGridDataColumn FieldName="Name" Caption="Location" MinWidth="100" Width="17%" />
                    <DxGridDataColumn FieldName="Year" DisplayFormat="0" MinWidth="80" Width="10%" />
                    <DxGridDataColumn FieldName="Population" DisplayFormat="#,0" MinWidth="100">
                        <FilterMenuTemplate>
                            <Grid_Filtering_ColumnFilterMenu_CustomRange FilterContext="context" Items="PopulationIntervals" />
                        </FilterMenuTemplate>
                    </DxGridDataColumn>
                    <DxGridDataColumn FieldName="Bedrooms" Width="12%">
                        <EditSettings>
                            <DxComboBoxSettings Data="BedroomDisplayValues" />
                        </EditSettings>
                    </DxGridDataColumn>
                    <DxGridDataColumn FieldName="Rent" MinWidth="80" DisplayFormat="C0">
                        <FilterMenuTemplate>
                            <Grid_Filtering_ColumnFilterMenu_CustomRange FilterContext="context" Items="RentIntervals" />
                        </FilterMenuTemplate>
                    </DxGridDataColumn>
                </Columns>
                <ToolbarTemplate>
                    <DxToolbar ItemRenderStyleMode="ToolbarRenderStyleMode.Plain" SizeMode="Params.SizeMode">
                        <Items>
                            <DxToolbarItem BeginGroup="true" Alignment="ToolbarItemAlignment.Left">
                                <Template Context="toolbar_item_context">
                                    <div class="d-flex flex-row align-items-center h-100">
                                        <DxCheckBox @bind-Checked="VirtualScrollingEnabled">Enable Virtual Scrolling</DxCheckBox>
                                    </div>
                                </Template>
                            </DxToolbarItem>
                        </Items>
                    </DxToolbar>
                </ToolbarTemplate>
            </DxGrid>
        </div>
        </DataProviderAccessAreaContainer>
        </ChildContentWithParameters>
        
        @code {
            static IReadOnlyList<CustomRangeFilterItem> PopulationIntervals { get; } = CustomRangeFilterItem.CreateIntervals("Population", 10, 6, false, "{0:N0}");
            static IReadOnlyList<CustomRangeFilterItem> RentIntervals { get; } = CustomRangeFilterItem.CreateIntervals("Rent", 200, 10, true, "{0:c}");
            static IReadOnlyList<string> BedroomDisplayValues { get; } = new[] { "Studio", "One Bedroom", "Two Bedrooms", "Three Bedrooms", "Four Bedrooms" };
            IRentInfoDataProvider RentInfoDataProvider { get; set; }
            RentInfoDataService RentInfoDataService { get; set; }

            bool VirtualScrollingEnabled { get; set; } = true;
            IGrid Grid { get; set; }
            object Data { get; set; }

            EntityInstantFeedbackSource InstantFeedbackSource { get; set; }

            protected override void OnInitialized() {
                // Refer to https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.components.owningcomponentbase
                RentInfoDataProvider = ScopedServices.GetRequiredService<IRentInfoDataProvider>();
                RentInfoDataService = ScopedServices.GetRequiredService<RentInfoDataService>();

                var connectionString = ConnectionStringUtils.GetGridLargeDataConnectionString(Configuration);
                if(string.IsNullOrEmpty(connectionString)) return;

                InstantFeedbackSource = new EntityInstantFeedbackSource(e => {
                    e.KeyExpression = "Oid";
                    e.QueryableSource = RentInfoDataService.GetAreaRentInfo();
                });
            }

            protected override async Task OnAfterRenderAsync(bool firstRender)
            {
                if (firstRender)
                {
                    await Grid.WaitForDataLoadAsync();
                    Grid.ExpandGroupRow(1);

                    await Grid.WaitForDataLoadAsync();
                    Grid.ExpandGroupRow(2);
                }
            }

            public void Dispose() {
                InstantFeedbackSource?.Dispose();
            }
        }
</DemoPageSectionComponent>
